﻿package net.wangit.dev.table;

import net.wangit.LabelUtil;
import net.wangit.context.ActionContext;
import net.wangit.context.ActionResult;
import net.wangit.dev.ConsoleDomainUtil;
import net.wangit.framework.config.DomainsSettings;
import net.wangit.jdbc.DBFactory;
import net.wangit.jdbc.DBImplementor;
import net.wangit.support.Action;
import net.wangit.util.Checker;

public class CreateTableAction implements Action {

	public CreateTableAction() {
	}

	public ActionContext execute(ActionContext ac) throws Exception {
		String script = ac.getRequestParameter("createtable.script");
		if (Checker.isEmpty(script))
			return ac;
		String domain = ConsoleDomainUtil.getDomain(ac);
		DBImplementor actionDB = (DBImplementor) DBFactory
				.getDB(DomainsSettings.getDatasource(domain));
		ActionResult result = ac.getActionResult();
		try {
			actionDB.execute(script);
			result.addInfo(LabelUtil.get(ac, "dev", "table.create.1"));
		} catch (Exception e) {
			result.setSuccessful(false);
			result.addError(LabelUtil.get(ac, "dev", "table.create.0"));
			throw e;
		}
		return ac;
	}
}
